<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\CarOwner;

use AuctionCore\Account\Back\Employee;
use AuctionCore\Other\Channel as Channel;
use AuctionCore\CarResource\CarModel;
use AuctionCore\CarResource\Car;
use AuctionCore\Model\AuCarOwner;

/**
 * 车主列表
 * @author 爱民
 */
class OwnerList extends \AuctionCore\System\ListBase
{

    /**
     * 获取列表结果
     * @param    int $page_number 每页条数
     * @param    int $page 页码
     * @return   Array
     */
    public function get($size, $page)
    {
        $m = new AuCarOwner();
        $m->where($this->condition);
        $m->field($this->fields);
        $m->order($this->order_by);
        //dump($m,true,null,false);
        if ($size < 1)
            $size = 1;
        if ($page < 2)
            $page = 1;
        $offset = ($page - 1) * $size;
        $m->limit($offset, $size);
        $ls = $m->select();
        return $ls;
    }

    public function count()
    {

    }

    /**
     * 获取车主列表
     * @param integer $page
     * @param integer $size
     * @param array $params
     * @return multitype:array(list,total)
     * @author anguolei@clcw.com.cn
     */
    public function getList($page = 1, $size = 10, $params, $order = 'owner_id desc')
    {

        switch ($params['status']) {
            case 2:
                $order = 'trace_type desc,modify_time desc';
                break;
            case 4:
                $order = 'check_fail_type desc,modify_time desc';
                break;
            default:
                break;
        }

        $map = $this->makeParams($params);
        $total = M('car_owner')->where($map)->count();
        $list = M('car_owner')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . "come_from ON comefrom=from_id")
            ->where($map)->limit($size)->page($page)->order($order)->select();
        // echo M()->getLastSql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            foreach ($list as $key => $val) {
                if ($val['brand_id'] || $val['series_id'] || $val['model_id']) {
                    $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                    $series = $cm->get_model_by_id($val['series_id'], 'series');
                    $model = $cm->get_model_by_id($val['model_id']);
                    $list[$key]['car_model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['year'] . ' ' . $model['name'];
                } else {
                    $list[$key]['car_model'] = $val['car_model'];
                }

                if ($params['status'] == 6) {
                    $car = Car::get_car_by_owner_id($val['owner_id']);
                    $list[$key]['car_no'] = $car['sno'];
                }

                if ($val['location_area']) {
                    $location_area = $addr->get_name_by_code($val['location_area']);
                    $list[$key]['location_area'] = $location_area;
                } else {
                    $list[$key]['location_area'] = '无';
                }
                //获取预约地址
                if ($val['service_method'] == 2) {
                    $province = $addr->get_name_by_code($val['reserve_province']);
                    $city = $addr->get_name_by_code($val['reserve_city']);
                    $area = $addr->get_name_by_code($val['reserve_area']);
                    $list[$key]['address'] = $province . " " . $city . " " . $area . " " . $val['reserve_address'];
                } elseif ($val['service_method'] == 3) {
                    //门店
                    $store = new \AuctionCore\Other\Store();
                    $map = array();
                    $map['store_id'] = array('eq', $val['reserve_store']);
                    $stores = $store->detail($map);
                    $list[$key]['address'] = $stores['store_name'];

                } else {
                    $list[$key]['address'] = "未知";
                }
                //转换时间
                $list[$key]['reserve_time'] = transform_date($val['reserve_time']);
                $checker = Employee::user_info_id($val['checker_id']);
                $myserver = Employee::user_info_id($val['service_id']);
                $list[$key]['checker_name'] = $checker['real_name'];
                $list[$key]['server_name'] = $myserver['real_name'];
                $list[$key]['phone_has_star'] = showHideMobile($val['phone']);
            }
        }

        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 拼装搜索条件
     * @param array $params
     * @return array $map
     * @author anguolei@clcw.com.cn
     */
    private function makeParams($params)
    {
        $map = array();

        //追踪类型
        if ($params['trace_type'] > -1) {
            $map['trace_type'] = array('eq', $params['trace_type']);
        }
        //$map['business_status'] = array('eq', 1);
        //业务状态
        if ($params['status'] == 1) {
            $map['business_status'] = array('eq', 1); //待预约检测
            $map['service_id'] = array('eq', 0);
        }
        if ($params['status'] == 2) {
            $map['business_status'] = array('eq', 2); //跟踪中
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            }
        }
        if ($params['status'] == 3) {
            $map['business_status'] = array('eq', 3); //预约失败
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            }
        }
        if ($params['status'] == 8) {
            $map['business_status'] = array('egt', 4); //卖车用户-预约成功
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            } else {
                $map['service_id'] = array('gt', 0);
            }
        }
        if ($params['status'] == 4) {
            $map['business_status'] = array('eq', 4); //预约成功-检测调度
            $map['is_assigned'] = 0;
            //$map['checker_id'] = array('eq', 0);
        }
        if ($params['status'] == 5) {
            $map['business_status'] = array('eq', 4); //待检测
            $map['is_assigned'] = 1;
            $map['checker_id'] = array('gt', 0);
        }

        if ($params['status'] == 6) {
            $map['business_status'] = array(array('egt', 5), array('neq', 6)); //检测成功
        }
        if ($params['status'] == 7) {
            //$map['business_status'] = array('eq', 6); //检测失败
            $map['_string'] = ' `business_status`=6 OR (`business_status`= 2 AND `check_fail_type`<4 AND `check_fail_type`>=1) ';
        }
        if ($params['status'] == 9) {
            $map['business_status'] = array('eq', 2);
            $map['trace_type'] = array('eq', 3); //滞留
        }

        if ($params['checker_id']) {
            $map['checker_id'] = array('eq', $params['checker_id']);
        }
        //检测失败类型
        if (!empty($params['fail_type'])) {
            $map['check_fail_type'] = array('eq', $params['fail_type']);
        }
        //品牌id
        if ($params['brand_id'] > 0) {
            $map['brand_id'] = array('eq', (int)$params['brand_id']);
        }
        //车系id
        if ($params['series_id'] > 0) {
            $map['series_id'] = array('eq', (int)$params['series_id']);
        }
        //车型搜索
//        if ($params['car_model']) {
//            $map['car_model'] = array('like', "%" . trim($params['car_model']) . "%");
//        }
        //车主id
        if ($params['owner_id']) {
            $owner_id = trim($params['owner_id']);
//            $map['owner_id'] = array('eq', $params['owner_id']);

            if ($params['tab'] == 1 || $params['tab'] == 5) {
                $map['owner_id'] = array('eq', $params['owner_id']);
            } else {
                $where = array();
                $where['owner_id'] = array('eq', $owner_id);
                $where['seller_name'] = array('LIKE', '%' . $owner_id . '%');
                $where['phone'] = array('LIKE', '%' . $owner_id . '%');
                $where['_logic'] = 'OR';
                $map['_complex'] = $where;
            }

        }
        //限定客服
        if (!empty($params['service_id'])) {
            $map['service_id'] = array('eq', (int)$params['service_id']);
        }


//        //车主姓名
//        if (!empty($params['seller_name'])) {
//            $map['seller_name'] = array('like', "%" . trim($params['seller_name']) . "%");
//        }
        //车主电话
        if (!empty($params['phone'])) {
            $map['phone'] = array("eq", trim($params['phone']));
        }
        //提交时间
        if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                $map['posttime'] = array("elt", $params['end_time'] . " 23:59:59");
            } elseif (empty($params['end_time'])) {
                $map['posttime'] = array("egt", $params['start_time'] . " 00:00:00");
            } else {
                $map['posttime'] = array(array("egt", $params['start_time'] . " 00:00:00"), array("lt", $params['end_time'] . " 23:59:59"));
            }
        }
        //检测时间
        if (!empty($params['check_date_start']) || !empty($params['check_date_end'])) {
            if (empty($params['check_date_start'])) {
                $map['check_date'] = array("elt", $params['check_date_end'] . " 23:59:59");
            } elseif (empty($params['check_date_end'])) {
                $map['check_date'] = array("egt", $params['check_date_start'] . " 00:00:00");
            } else {
                $map['check_date'] = array(array("egt", $params['check_date_start'] . " 00:00:00"), array("lt", $params['check_date_end'] . " 23:59:59"));
            }
        }
        //检测失败时间
        if (!empty($params['check_start_time']) || !empty($params['check_end_time'])) {
            if (empty($params['check_start_time'])) {
                $map['check_fail_date'] = array("elt", $params['check_end_time'] . " 23:59:59");
            } elseif (empty($params['check_end_time'])) {
                $map['check_fail_date'] = array("egt", $params['check_start_time'] . " 00:00:00");
            } else {
                $map['check_fail_date'] = array(array("egt", $params['check_start_time'] . " 00:00:00"), array("lt", $params['check_end_time'] . " 23:59:59"));
            }
        }
        //预约时间
        if (!empty($params['reserve_start_time']) || !empty($params['reserve_end_time'])) {
            if (empty($params['reserve_start_time'])) {
                $map['reserve_time'] = array("elt", $params['reserve_end_time'] . " 23:59:59");
            } elseif (empty($params['reserve_end_time'])) {
                $map['reserve_time'] = array("egt", $params['reserve_start_time'] . " 00:00:00");
            } else {
                $map['reserve_time'] = array(array("egt", $params['reserve_start_time'] . " 00:00:00"), array("elt", $params['reserve_end_time'] . " 23:59:59"));
            }
        }
        //跟踪时间
        if (!empty($params['trace_start_time']) || !empty($params['trace_end_time'])) {
            if (empty($params['trace_start_time'])) {
                $map['next_trace_time'] = array("elt", $params['trace_end_time'] . " 23:59:59");
            } elseif (empty($params['trace_end_time'])) {
                $map['next_trace_time'] = array("egt", $params['trace_start_time'] . " 00:00:00");
            } else {
                $map['next_trace_time'] = array(array("egt", $params['trace_start_time'] . " 00:00:00"), array("elt", $params['trace_end_time'] . " 23:59:59"));
            }
        }

        //预约省份
        if (!empty($params['reserve_province']) && is_numeric($params['reserve_province'])) {
            $map['reserve_province'] = array('eq', (int)$params['reserve_province']);
        }
        //预约市
        if (!empty($params['reserve_city']) && is_numeric($params['reserve_city'])) {
            $map['reserve_city'] = array('eq', (int)$params['reserve_city']);
        }
        //预约地区
        if (!empty($params['reserve_area']) && is_numeric($params['reserve_area'])) {
            $map['reserve_area'] = array('eq', (int)$params['reserve_area']);
        }
        //车辆所在地
        if (!empty($params['location_area'])) {
            $map['location_area'] = array('in', $params['location_area']);
        }
        //父渠道来源
        if ($params['from'] != 0 && $params['comefrom'] == '-2') {
            $channel = new Channel($params['from']);
            $channel_children = $channel->get_children_channel_from_id_list();
            $map['comefrom'] = array('in', $channel_children);
        }
        //子渠道来源
        if ($params['comefrom'] !== null && $params['comefrom'] > -2) {
            $map['comefrom'] = array('eq', $params['comefrom']);
        }
        return $map;
    }

    public function getWaitingCheckList($page, $size, $params)
    {
        $order = 'car_owner.modify_time DESC';

        $map = $this->makeWaitingCheckListParams($params);

        $fields = 'cars.car_id,cars.car_no,cars.sid,cars.sno,cars.plate_prefix,cars.plate_suffix,car_owner.*, come_form.from_name';
        $total = M('breach_car')->alias('bc')
	        ->join('LEFT JOIN ' . C('DB_PREFIX') . 'cars as cars ON bc.car_id = cars.car_id')
	        ->join('LEFT JOIN ' . C('DB_PREFIX') . 'car_owner as car_owner ON cars.car_id = car_owner.old_car_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . "come_from as come_form ON car_owner.comefrom = come_form.from_id")
            ->where($map)->count();

        $list = M('breach_car')->alias('bc')
	        ->join('LEFT JOIN ' . C('DB_PREFIX') . 'cars as cars ON bc.car_id = cars.car_id')
	        ->join('LEFT JOIN ' . C('DB_PREFIX') . 'car_owner as car_owner ON cars.car_id = car_owner.old_car_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . "come_from as come_form ON car_owner.comefrom = come_form.from_id")
            ->where($map)->limit($size)->page($page)->order($order)
            ->field($fields)
            ->select();
		//echo M()->getLastSql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            foreach ($list as $key => $val) {
                if ($val['brand_id'] || $val['series_id'] || $val['model_id']) {
                    $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                    $series = $cm->get_model_by_id($val['series_id'], 'series');
                    $model = $cm->get_model_by_id($val['model_id']);
                    $list[$key]['car_model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['year'] . ' ' . $model['name'];
                } else {
                    $list[$key]['car_model'] = $val['car_model'];
                }

                if ($params['status'] == 6) {
                    $car = Car::get_car_by_owner_id($val['owner_id']);
                    $list[$key]['car_no'] = $car['car_no'];
                }

                if ($val['location_area']) {
                    $location_area = $addr->get_name_by_code($val['location_area']);
                    $list[$key]['location_area'] = $location_area;
                } else {
                    $list[$key]['location_area'] = '无';
                }

                //车牌号
                $list[$key]['plate_number'] = C("PLATE_PREFIX." . $val['plate_prefix']) . $val['plate_suffix'];
                //获取预约地址
                if ($val['service_method'] == 2) {
                    $province = $addr->get_name_by_code($val['reserve_province']);
                    $city = $addr->get_name_by_code($val['reserve_city']);
                    $area = $addr->get_name_by_code($val['reserve_area']);
                    $list[$key]['address'] = $province . " " . $city . " " . $area . " " . $val['reserve_address'];
                } elseif ($val['service_method'] == 3) {
                    //门店
                    $store = new \AuctionCore\Other\Store();
                    $map = array();
                    $map['store_id'] = array('eq', $val['reserve_store']);
                    $stores = $store->detail($map);
                    $list[$key]['address'] = $stores['store_name'];

                } else {
                    $list[$key]['address'] = "未知";
                }
                //转换时间
                $list[$key]['reserve_time'] = transform_date($val['reserve_time']);
                $checker = Employee::user_info_id($val['checker_id']);
                $myserver = Employee::user_info_id($val['service_id']);
                $list[$key]['checker_name'] = $checker['real_name'];
                $list[$key]['checker_user_mobile'] = $checker['mobile'];
                $list[$key]['server_name'] = $myserver['real_name'];
                $list[$key]['phone_has_star'] = showHideMobile($val['phone']);
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    public function makeWaitingCheckListParams($params)
    {
        $map['car_owner.business_status'] = array('elt', 4); //待预约检测
        //车辆所在地
        if (!empty($params['location_area'])) {
            $map['car_owner.location_area'] = array('in', $params['location_area']);
        }
	    $pay_status = $params['pay_status'];
	    if ($pay_status == 2) {
		    $map['_string'] = 'cars.pay_status = 2 OR (cars.pay_status = 3 AND cars.three_in_one = 2)';
	    } elseif ($pay_status == 1) {
		    $map['cars.pay_status'] = ['eq', $pay_status];
	    }

        if (!empty($params['sno'])) {
            $map['cars.sno'] = array('eq', $params['sno']);
        }
        $map['car_owner.old_car_id'] = array('neq', '');
        return $map;
    }

    /**
     * 获取卖车用户列表
     * @param integer $page
     * @param integer $size
     * @param array $params
     * @return multitype:array(list,total)
     * @author xiangminghu@clcw.com.cn
     */
    public function get_carowner_list($page = 1, $size = 10, $params)
    {
        //$car = new CarModel;
        $map = array();
        if ($params['start_time']) {
            $map['posttime'] = array('egt', $params['start_time'] . " 00:00:00");
        }
        if ($params['end_time']) {
            $map['posttime'] = array('elt', $params['end_time'] . " 23:59:59");
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['posttime'] = array(array('egt', $params['start_time'] . " 00:00:00"), array('elt', $params['end_time'] . " 23:59:59"));
        }
        if ($params['car_model']) {
            $car_model = trim($params['car_model']);
            $map['car_model'] = array('LIKE', '%' . $car_model . '%');
        }
//        if ($params['brand_id'] > 0) {
//            //根据主品牌id获取子品牌
//            $brand_info = $car->get_brand_by_mbrand($params['brand_id']);
//            if (!empty($brand_info)) {
//                foreach ($brand_info as $v1) {
//                    $brand_list[] = $v1['brand_id'];
//                }
//                if (!empty($brand_list)) {
//                    $map['brand_id'] = array("in", $brand_list);
//                }
//            }
//        }
        //车系
//        if ($params['series_id'] > 0) {
//            $map['series_id'] = array("eq", (int) $params['series_id']);
//        } else {
//            if ($params['brand_id']) {
//                //根据主品牌id获取子品牌
//                $brand_info = $car->get_brand_by_mbrand($params['brand_id']);
//                if (!empty($brand_info)) {
//                    foreach ($brand_info as $v1) {
//                        $brand_list[] = $v1['brand_id'];
//                    }
//                }
//                $series_info = $car->get_series_by_mbrand($params['brand_id']);
//                if (!empty($series_info)) {
//                    foreach ($series_info as $v2) {
//                        $series_list[] = $v2['series_id'];
//                    }
//                    if (!empty($series_list)) {
//                        $map['series_id'] = array('in', $series_list);
//                    }
//                }
//            }
//        }
        //车辆所在地
        if (!empty($params['location_area'])) {
            $map['location_area'] = array('in', $params['location_area']);
        }
        if ($params['status'] == 1) {
            $map['business_status'] = array('eq', 1); //待预约检测
            $map['service_id'] = array('eq', 0);
        }
        if ($params['status'] == 2) {
            $map['business_status'] = array('eq', 2); //跟踪预约
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            }
        }
        if ($params['status'] == 3) {
            $map['business_status'] = array('eq', 3); //预约失败
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            }
        }
        if ($params['status'] == 4) {                   //待分配
            $map['business_status'] = array('eq', 4); //卖车用户-预约成功
            $map['is_assigned'] = 0;
//            $map['business_status'] = array('egt', 4); //卖车用户-预约成功
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            }
        }
        if ($params['status'] == 7) {
            $map['business_status'] = array('eq', 4);
            $map['is_assigned'] = 1;
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            }
        }
        if ($params['status'] == 5) {
            $map['business_status'] = array(array('egt', 5), array("neq", 6)); //检测成功
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            }
        }
        if ($params['status'] == 6) {
            $map['business_status'] = array('eq', 6); //检测失败
            if (!$params['auth']) {
                $map['service_id'] = array('eq', $params['emp_id']);
            }
        }
        if ($params['seller_name']) {
            $seller_name = trim($params['seller_name']);
            $map['seller_name'] = array("LIKE", '%' . $seller_name . '%');
//            $map['_string'] = "o.seller_name LIKE '%" . $seller_name . "%' OR e.real_name LIKE '%" . $seller_name . "%'";
        }

        //来源
        if ($params['comefrom'] != '-2') {
            $map['comefrom'] = array('eq', $params['comefrom']);
        }

        //客服和评估师
        if ($params['server'] && empty($params['checker'])) {
            $map['service_id'] = array('eq', $params['server']);
        }
        if (empty($params['server']) && $params['checker']) {
            $map['checker_id'] = array('eq', $params['checker']);
        }
        if ($params['server'] && $params['checker']) {
            $map['_string'] = 'service_id=' . $params['server'] . ' AND checker_id=' . $params['checker'];
        }

        $total = M('car_owner')->where($map)->count();
        $list = M('car_owner')->where($map)->limit($size)->page($page)
            ->order("owner_id desc")->select();
        //echo M()->getLastSql();
        if (!empty($list)) {
            $addr = new \AuctionCore\Other\Address();
            $cm = new CarModel();
            foreach ($list as $key => $val) {
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $province = $addr->get_name_by_code($val['reserve_province']);
                $city = $addr->get_name_by_code($val['reserve_city']);
                $area = $addr->get_name_by_code($val['reserve_area']);
                $location_area = $addr->get_name_by_code($val['location_area']);
                $checker = Employee::user_info_id($val['checker_id']);
                $myserver = Employee::user_info_id($val['service_id']);

                $car_model = trim($brand['name'] . ' ' . $series['name']);
                $list[$key]['seller_name'] = $val['seller_name'];
                $list[$key]['server_id'] = $val['service_id'];
                $list[$key]['model_show'] = $car_model ? $car_model : $val['car_model'];
                $list[$key]['checker_name'] = $checker['real_name'];
                $list[$key]['server_name'] = $myserver['real_name'];
                $list[$key]['location_area'] = $location_area;
                $list[$key]['business_status'] = $val['business_status'];
                $business_status = $val['business_status'];
                if ($business_status == 1 && $val['service_id'] == 0) {           //待预约
                    $business_status_code = 1;
                } elseif ($business_status == 4 && $val['is_assigned'] == 0) {     //待分配
                    $business_status_code = 4;
                } elseif ($business_status == 4 && $val['is_assigned'] == 1) {     //待检测
                    $business_status_code = 5;
                } elseif ($business_status >= 5 && $business_status != 6) {     //检测成功
                    $business_status_code = 6;
                } elseif ($business_status == 6) {     //检测失败
                    $business_status_code = 7;
                } else {
                    $business_status_code = $business_status;
                }
                $list[$key]['business_status_name'] = C('CAR_BUSINESS_STATUS')[$business_status_code];
                $list[$key]['posttime'] = $val['posttime'];
                $list[$key]['address'] = $province . " " . $city . " " . $area . " " . $val['reserve_address'];
                $list[$key]['phone_has_star'] = showHideMobile($val['phone']);
            }
        }

        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

}
